package com.company.test30;

import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;
import java.util.RandomAccess;

/**
 * Created by v on 16-12-1.
 */
public class Test30_05 {
    private RandomAccessFile raf;
    private int count;

    public static void main(String[] args) {
        new Test30_05();
    }

    public Test30_05(){
        try {
            ServerSocket serverSocket=new ServerSocket(8000);
            System.out.println("Server started ");

            raf=new RandomAccessFile("count.dat","rw");

            if(raf.length()==0){
                count=0;
            }else{
                count=raf.readInt();
            }

            while (true){
                Socket socket=serverSocket.accept();

                DataOutputStream outputToClient=
                        new DataOutputStream(socket.getOutputStream());

                count++;
                outputToClient.writeInt(count);

                raf.seek(0);
                raf.writeInt(count);
            }

        }catch (IOException ex){
            ex.printStackTrace();
        }
    }
}
